package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
public class SetmealController {
    @Autowired
    SetmealService setmealService;

    //分页查询
    @GetMapping("/setmeal/page")
    public ResultInfo selectByLimit(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
            String name){
        Page<Setmeal> selectByLimit = setmealService.selectByLimit(page,pageSize,name);

        return ResultInfo.success(selectByLimit);
    }

    //回显
    @GetMapping("/dish/list")
    public ResultInfo selectByCateGoryId(Long categoryId,String name){
        List<Dish> list = setmealService.selectByCateGoryId(categoryId,name);
        return ResultInfo.success(list);
    }

    //添加
    @CacheEvict(value = "setmeal",key = "#setmeal.categoryId")
    @PostMapping("/setmeal")
    public ResultInfo add(@RequestBody Setmeal setmeal){
        setmealService.add(setmeal);
        return ResultInfo.success();
    }

    //修改的回显
    @GetMapping("/setmeal/{id}")
    public ResultInfo findById(@PathVariable Long id){
        Setmeal setmeals = setmealService.findById(id);
        return ResultInfo.success(setmeals);
    }

    //修改
    @CacheEvict(value = "setmeal",allEntries = true)
    @PutMapping("/setmeal")
    public ResultInfo update(@RequestBody Setmeal setmeal){
        setmealService.update(setmeal);
        return ResultInfo.success();
    }

    //禁用，启用
    // /setmeal/status/0?ids=1632721782066442241
    @CacheEvict(value = "setmeal",allEntries = true)
    @PostMapping("/setmeal/status/{status}")
    public ResultInfo updateStatusSetmeal(@PathVariable Integer status,@RequestParam List<Long> ids){
        setmealService.updateStatusSetmeal(status,ids);
        return ResultInfo.success();
    }

    //删除
    @DeleteMapping("/setmeal")
    @CacheEvict(value = "setmeal",allEntries = true)
    public ResultInfo delete(@RequestParam List<Long> ids){
        setmealService.delete(ids);
        return ResultInfo.success();
    }

}
